
package com.scs.application.modules.base.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.pdf.PdfExportUtils;
import com.scs.application.core.pdf.entity.PdfExportParams;
import com.scs.application.core.pdf.enums.PdfPageDirection;
import com.scs.application.core.pdf.enums.SpacingMode;
import com.scs.application.core.utils.FileUtils;
import com.scs.application.core.utils.FreeMarkers;
import com.scs.application.modules.base.entity.MaterialBid;
import com.scs.application.modules.base.entity.MaterialBidItem;
import com.scs.application.modules.base.request.MaterialBidQueryRequest;
import com.scs.application.modules.base.service.MaterialBidItemService;
import com.scs.application.modules.base.service.MaterialBidService;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 耗材报价单 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/base/material_bids")
public class MaterialBidController extends CrudController<MaterialBid, String, MaterialBidService> implements ImAndExAbleController<MaterialBidQueryRequest> {

    @Autowired
    private final MaterialBidItemService materialBidItemService;

    @GetMapping("/page")
    public PageResult page(MaterialBidQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(MaterialBidQueryRequest request) {
        List<MaterialBid> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    /**
     * 供应商提交报价单
     *
     * @param id
     * @return
     */
    @PostMapping("/submit_material_bid/{id}")
    public CommonResult submitMaterialBid(@PathVariable("id") String id) {
        service.submitMaterialBid(id);
        return CommonResult.ok("提交成功");
    }

    /**
     * 供应商撤销报价单
     *
     * @param id
     * @return
     */
    @PostMapping("/cancel_material_bid/{id}")
    public CommonResult cancelMaterialBid(@PathVariable("id") String id) {
        service.cancelMaterialBid(id);
        return CommonResult.ok("撤销成功");
    }

    /**
     * PDF打印预览
     *
     * @param id
     * @param response
     */
    @GetMapping("/{id}/pdf-preview")
    public void pdfPreview(@PathVariable(name = "id") String id,
                           @RequestParam(name = "direction", required = false, defaultValue = "transverse") String direction,
                           @RequestParam(name = "spacingMode", required = false, defaultValue = "normal") String spacingMode,
                           @RequestParam(name = "pageSize", required = false, defaultValue = "A4") String pageSize,
                           @RequestParam(name = "perRowsOfGrid", required = false, defaultValue = "5") int perRowsOfGrid,
                           HttpServletResponse response) throws IOException {

        MaterialBid materialBid = service.getById(id);
        if (materialBid == null) {
            return;
        }

        PdfPageDirection pageDirection = PdfPageDirection.of(direction);
        SpacingMode sMode = SpacingMode.of(spacingMode);
        String path = String.format("print-template/material_bid/%s.json", pageSize.toUpperCase());

        String templateJson = FileUtils.readClassPathFileToString(path, "UTF-8");
        response.setContentType(MediaType.APPLICATION_PDF_VALUE);
        response.addHeader("Content-Disposition", String.format("filename=%s.pdf", URLEncoder.encode("报价单")));

        Map<String, Object> map = Maps.newHashMap();
        map.put("materialBid", materialBid);

        templateJson = FreeMarkers.renderString(templateJson, map);
        PdfExportParams exportParams = PdfExportParams.fromJson(templateJson);

        exportParams.setSpacingMode(sMode);
        exportParams.setPageDirection(pageDirection);
        exportParams.setPerRowsOfGrid(perRowsOfGrid);

        List<MaterialBidItem> items = materialBidItemService.list(Wrappers.<MaterialBidItem>query().eq("bid_id", id));
        exportParams.setGridData(items);

        PdfExportUtils.export(response.getOutputStream(), exportParams);
    }


}
